package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.util.AliyunOSSUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService{
    @Autowired
    private DtsBrandMapper brandMapper;
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Override
    public IPage<DtsBrand> queryBrandAll(Integer page, Integer limit, String sort, String order) {
        IPage<DtsBrand> iPage=new Page<>(page,limit);
        QueryWrapper<DtsBrand> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                queryWrapper.orderByDesc(sort);
            }
        }
        IPage<DtsBrand> brandIPage = brandMapper.selectPage(iPage, queryWrapper);
        return brandIPage;
    }

    /**
     * 一级类目
     * @return
     */
    @Override
    public List<CatVo> queryBrand() {
        List<CatVo> catVos = brandMapper.queryBrand();
        return catVos;
    }

    /**
     * 二级类目
     * @return
     */
    @Override
    public List<DtsCategory> queryCategory() {
        List<DtsCategory> dtsCategories = brandMapper.queryCategory();
        return dtsCategories;
    }

    @Override
    public DtsBrand addBrand(DtsBrand dtsBrand) {
        dtsBrand.setAddTime(new Date());
        dtsBrand.setDeleted(false);
        dtsBrand.setUpdateTime(new Date());
        brandMapper.insert(dtsBrand);
        return dtsBrand;
    }

    @Override
    public int updateBrand(DtsBrand dtsBrand) {
        dtsBrand.setUpdateTime(new Date());
        int updateById = brandMapper.updateById(dtsBrand);

        return updateById;
    }

    @Override
    public void deleteBrand(DtsBrand dtsBrand) {

        String picUrl = dtsBrand.getPicUrl();
        int indexOf = picUrl.lastIndexOf("/");
        int indexOf1 = picUrl.lastIndexOf("?");
        String uuid = picUrl.substring(indexOf+1, indexOf1);
        AliyunOSSUtil.deleteFile(endpoint,accessKeyId,accessKeySecret,bucketName,uuid);


        QueryWrapper<DtsBrand> queryWrapper=new QueryWrapper<>();
        Integer id = dtsBrand.getId();
        queryWrapper.eq("id",id);
        brandMapper.delete(queryWrapper);

    }
}
